package com.yeyuanqi.hottop.untils;

import com.yeyuanqi.hottop.entity.SysWeb;
import com.yeyuanqi.hottop.service.RedisService;
import com.yeyuanqi.hottop.service.SysWebService;
import com.yeyuanqi.hottop.vo.NameAndValueVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 封装了一个用于生成日历饼图所需数据格式的方法
 * @author yeyuanqi
 * @date 2022/01/19
 **/
@Component
public class CalendarPieDataUtil {

    @Autowired
    private RedisService redisService;

    @Autowired
    private SysWebService sysWebService;

    /**
     * 从redis获取的每日热搜top1数据转换成list<list<>>类型
     * 用于日历饼图
     * @return 返回一个List<List<NameAndValueVO>>列表
     */
    public List getMonthlyTopOneData(){
        String date;
        List<List> dataList = new ArrayList<>();
        //创建日期类，默认为当天
        Calendar calendar = Calendar.getInstance();
        //获取本月1号到今天的天数
        int days = calendar.get(Calendar.DATE);
        //获取现在的日期
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        //获取本月1号到今天的天数的所有平台top1的数据
        for (int i=0;i<days;i++) {
            //i=0，获取今天的数据
            if (i==0){
                calendar.add(Calendar.DATE, 0);
                date = df.format(calendar.getTime());
            } else {
                //i!=0,-1倒退一天获取数据
                calendar.add(Calendar.DATE, -1);
                date = df.format(calendar.getTime());
            }
            List<NameAndValueVO> dailtdataList = new ArrayList<>();


            List<SysWeb> webinfolists = sysWebService.list();
            for (SysWeb list :webinfolists){
                if (list.getIsshow()==1){
                    switch (list.getWebname()){
                        case "微博热搜":
                            String wbrediskey = date + "-weibodailytop";
                            NameAndValueVO wbdata = redisService.zGetMaxScore(wbrediskey);
                            dailtdataList.add(wbdata);
                            break;
                        case "今日头条":
                            String jrrediskey = date + "-jinridailytop";
                            NameAndValueVO jrdata = redisService.zGetMaxScore(jrrediskey);
                            dailtdataList.add(jrdata);
                            break;
                        case "腾讯新闻":
                            String txrediskey = date + "-tengxundailytop";
                            NameAndValueVO txdata = redisService.zGetMaxScore(txrediskey);
                            dailtdataList.add(txdata);
                            break;
                        case "百度热搜":
                            //从redis中获取top1的数据,并封装成列表
                            String bdrediskey = date + "-baidudailytop";
                            NameAndValueVO bddata = redisService.zGetMaxScore(bdrediskey);
                            dailtdataList.add(bddata);
                            break;
                    }
                }
            }
            //封装进最外层列表
            dataList.add(dailtdataList);
        }
        //反转列表
        Collections.reverse(dataList);
        return dataList;
    }


    /**
     * 从redis获取的每日热搜top1数据转换成list<list<>>类型
     * @return 返回一个List<List<NameAndValueVO>>列表
     */
    public List getTopOneDataByMonth(String yearMonth){

        String date;
        int year = Integer.parseInt(yearMonth.split("-")[0]);  //年
        int month = Integer.parseInt(yearMonth.split("-")[1]); //月

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        List<List> dataList = new ArrayList<>();

        // 设置年份
        calendar.set(Calendar.YEAR, year);
        // 设置月份,不-1则设置为month的下一个月
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = calendar.getActualMaximum(Calendar.DATE);
        // 设置日期为该月份第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        //获取改约所有平台top1的数据
        for (int i=0;i<lastDay;i++) {
            //获取每天的日期，yyyy-mm-dd格式
            if (i==0){
                calendar.add(Calendar.DATE, 0);
                date = df.format(calendar.getTime());
            } else {
                calendar.add(Calendar.DATE, +1);
                date = df.format(calendar.getTime());
            }
            List<NameAndValueVO> dailtdataList = new ArrayList<>();

            //从redis中获取top1的数据,并封装成列表
            String bdrediskey = date + "-baidudailytop";
            NameAndValueVO bddata = redisService.zGetMaxScore(bdrediskey);
            dailtdataList.add(bddata);

            String jrrediskey = date + "-jinridailytop";
            NameAndValueVO jrdata = redisService.zGetMaxScore(jrrediskey);
            dailtdataList.add(jrdata);

            String txrediskey = date + "-tengxundailytop";
            NameAndValueVO txdata = redisService.zGetMaxScore(txrediskey);
            dailtdataList.add(txdata);

            String wbrediskey = date + "-weibodailytop";
            NameAndValueVO wbdata = redisService.zGetMaxScore(wbrediskey);
            dailtdataList.add(wbdata);

            //封装进最外层列表
            dataList.add(dailtdataList);
        }
        return dataList;
    }
}
